method_exists
检查类的方法是否存在
PHP 4 及以上版本
method_exists() 用于检查对象是否具有指定的方法。
bool method_exists(object $object, string $method_name);
返回布尔值,如果指定的对象具有该方法,返回 true,否则返回 false。
以下是使用 method_exists 的示例:
<?php
class MyClass {
public function myMethod() {
echo "This is myMethod!";
}
}
$obj = new MyClass();
if (method_exists($obj, 'myMethod')) {
echo 'Method exists!';
} else {
echo 'Method does not exist!';
}
?>
在这个示例中,创建了一个名为 MyClass 的类,并定义了一个名为 myMethod 的方法。然后通过 method_exists 检查对象 $obj 是否具有 myMethod 方法。如果存在该方法,则输出 "Method exists!",否则输出 "Method does not exist!"。